<?php
$this->pageTitle = 'Khóa sinh viên';
$this->breadcrumbs = array(
	'Khóa sinh viên',
);


?>
<div class="heading">
    <h1><img src="<?php echo Util::themeUrl() ?>/images/header.ico" /> <?php echo $this->pageTitle; ?></h1>

    <div class="controls">
        <?php
        echo CHtml::button('Đăng kí', array('class' => 'controls', 'id' => 'create-courses'));
        echo CHtml::button('Xóa', array('class' => 'controls', 'id' => 'delete-courses'));
        ?>
    </div>
</div>

<div class="content">

<?php
// echo CHtml::link('Advanced Search','#',array('class'=>'search-button'));
// echo '<div class="search-form" style="display:none">';
// $this->renderPartial('_search',array(
//	'model'=>$model,
// ));
// echo '</div><!-- search-form -->';
?>


<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'courses-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
    'cssFile' => Util::themeUrl() . '/css/gridview.css',
    'htmlOptions'=>array('class'=>'grid-view clear'),
    'afterAjaxUpdate'=>'js:function(id,data){$.bind_data()}',
	'columns'=>array(
		array(
            'header' =>  'No.',
            'value' => '$row + Util::getPaggingIndex()',
            'htmlOptions' => array(
                'style' => 'width:25px; text-align:center'
            )
        ),
        array(
            'class' => 'CCheckBoxColumn',
            'selectableRows' => 2,
            'id' => 'chk',
            'value' => '$data->id',
        ),
        array(
			'name'	=>'id',
			'type'	=>'raw',
			'value'	=>'Util::markSearch($data,"id")',
        	'htmlOptions' => array(
                'style' => 'width:80px; text-align:center'
            )
		),
		array(
			'name'	=>'name',
			'type'	=>'raw',
			'value'	=>'Util::markSearch($data,"name")',
			'htmlOptions' => array(
                'style' => 'width:120px;'
            )
		),
		array(
			'name'	=>'note',
			'type'	=>'raw',
			'value'	=>'Util::markSearch($data,"note")',
		),
		array(
			'name'			=>'status',
	      	'type' 			=> 'raw',
	      	'value' 		=> 'CHtml::link(CHtml::image(Util::themeUrl(). "/images/" . (($data->status==STATUS_ACTIVE)?"tick_circle.png":"cross_circle.png")), array("update_status", "id"=>$data->id, "returnUrl" => "index"), array("class" => "update-status"))',
			'filter' 		=> Util::itemAlias('Status'),
	      	'htmlOptions' 	=> array(
                'style' => 'width:90px;text-align:center',
            )
	),
		array(
			'class'=>'CButtonColumn',
			'template' => '{view}{update}',
			'htmlOptions' => array(
                'style' => 'width:50px;text-align:center',
            )

		),
	),
)); ?>
</div>

<script  type="text/javascript">
    $(document).ready(function() {
        $. bind_data = function(){
            $(".grid-view a.view").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('courses-grid')
                    }
                });
                return false;
            });

            $(".grid-view a.update").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('courses-grid')
                    }
                });
                return false;
            });
        }

        // bind data
        $. bind_data();

        $("#create-courses").click(function() {
                $.fancybox.open({
                    href : '<?php echo $this->createUrl('create', array('ajax' => true)) ?>',
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('courses-grid')
                    }
                });
                return false;
            });

        $('#delete-courses').live('click', function(){
            var count=0;
            var params = 'YII_CSRF_TOKEN=<?php echo  Yii::app()->request->csrfToken; ?>';
            $("input[name='chk[]']").each(function(){
            if($(this).is(":checked")){
                count++;
                params += '&chk[]=' + $(this).val();
            }
            });
            if(count >= 1 ){
            if(!confirm("Xóa dữ liệu được chọn?"))
                return false;
            }else{
                alert("Chưa chọn dữ liệu muốn xóa.");
                return false;
            }

            $.ajax({
                    type: "POST",
                    url: '<?php echo $this->createUrl('delete', array('ajax' => true)) ?>',
                    data: params,
                    success: function(data) {
                    	alert('Dữ liệu xóa thành công.');
                        $.fn.yiiGridView.update('courses-grid');
                    } //success
                });//ajax
                return false;
        });
    });
</script>